package whu.o2o;

import javax.crypto.spec.IvParameterSpec;

import whu.o2o.View.LoadDialog;
import whu.o2o.util.ConstantData;
import whu.o2o.util.ImeUtil;
import whu.o2o.util.Utils;

import whu.o2o.R;
import whu.o2o.connection.CommonHttpConnect;
import whu.o2o.connection.Params;





import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class RetsetPasswordActivity extends BaseActivity implements OnClickListener{
	private EditText et_phone;
	private EditText et_yzm;
	private EditText et_password;
	private EditText et_passwordconfig;
	private ImageButton ib_submit;
	private Button ib_hqyzm;
	private ImageButton ib_back;
    private String phone,password,passwordconfig,yzm;
    private Context mcontext;
    int mPositiveCount;
    int count = 60;
//	TipDialog tipDialog = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resetpassword_activity);
		mcontext = this;
		initView();
	}
	
	private void initView(){
		ib_back = (ImageButton) findViewById(R.id.ib_resetpassword_back);
		ib_submit = (ImageButton) findViewById(R.id.ib_resetpassword_submit);
		ib_hqyzm = (Button) findViewById(R.id.ib_resetpassword_hqyzm);
		et_phone = (EditText) findViewById(R.id.et_resetpassword_username);
		et_password = (EditText) findViewById(R.id.et_resetpassword_password);
		et_passwordconfig = (EditText) findViewById(R.id.et_resetpassword_passwordconfig);
		et_yzm = (EditText) findViewById(R.id.et_resetpassword_yzm);
		ib_back.setOnClickListener(this);
		ib_submit.setOnClickListener(this);
		ib_hqyzm.setOnClickListener(this);
	}
	
	// �޸������ύ�ж�
	private void updatePassword() {
		phone = et_phone.getText().toString().trim();
		yzm = et_yzm.getText().toString().trim();
		password = et_password.getText().toString().trim();
		passwordconfig = et_passwordconfig.getText().toString().trim();
	
		if (TextUtils.isEmpty(phone)||Utils.isMobileNO(phone)) {
			
			ImeUtil.showToast(this, "请输入真实电话号码", 2000);
			return;
		} else if (TextUtils.isEmpty(yzm)) {
			ImeUtil.showToast(this, "请输入验证码", 2000);
			return;
		} else if(TextUtils.isEmpty(password)){
			ImeUtil.showToast(this, "请输入密码", 2000);
			return;
		} else if (TextUtils.isEmpty(passwordconfig)) {
			ImeUtil.showToast(this, "请输入确认密码", 2000);
			return;
		}  else if (!password.equals(
				passwordconfig)) {
			ImeUtil.showToast(this, "您两次输入的密码不一致", 2000);
			return;
		}
		requestData();
	}
	
	/**
	 * ��֤�뵹��ʱ
	 */
	private void validateEnable() {
		count = 60;
		ib_hqyzm.setText("重新获取(" + count + ")");
		ib_hqyzm.setTextColor(getResources().getColor(R.color.littlegray));
		ib_hqyzm.setEnabled(false);
		mPositiveCount = count;
		mHandler.sendEmptyMessageDelayed(1, 1000);
	}
	
	private void requestData() {
		int userId = mApplication.getUserId();

		Params params = new Params();
	
			params.setUrl(String.format(ConstantData.Url.POST_UPDATEPASSWORD,
					userId,phone,yzm,password,passwordconfig));
	
		params.setHandler(mHttpHandler);
		params.setRequestType(ConstantData.Data.POST_HTTP);
		params.setMsgCode(ConstantData.Data.GET_HTTP_RESULT_MSG_CODE);
		params.setActivity(this);
		params.setShowLoad(true);

		LoadDialog.showDailog(this);
		mApplication.getThreadPool().execute(new CommonHttpConnect(params));
	}
	
	private void requestCode(){
		int userId = mApplication.getUserId();

		Params params = new Params();
	
			params.setUrl(String.format(ConstantData.Url.GET_CODE,
					userId,phone));
	
		params.setHandler(mHandler);
		params.setRequestType(ConstantData.Data.GET_HTTP);
		params.setMsgCode(ConstantData.Data.GET_HTTP_RESULT_MSG_CODE);
		params.setActivity(this);
		params.setShowLoad(true);
	

		LoadDialog.showDailog(this);
		mApplication.getThreadPool().execute(new CommonHttpConnect(params));
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch (v.getId()) {
		case R.id.ib_resetpassword_hqyzm:
			requestCode();
			validateEnable();
			break;

		case R.id.ib_resetpassword_back:
			finish();
			break;
			
		case R.id.ib_resetpassword_submit:
			updatePassword();
			
			break;
			


		}
		
	}
	
	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				if (mPositiveCount >= 2) {
					mPositiveCount--;
					if (ib_hqyzm != null) {
						ib_hqyzm.setText("重新获取(" + mPositiveCount + ")");
					}
					mHandler.sendEmptyMessageDelayed(1, 1000);
				} else {
					if (ib_hqyzm != null) {
						if (!ib_hqyzm.isEnabled()) {
							ib_hqyzm.setEnabled(true);
							ib_hqyzm.setTextColor(getResources().getColor(R.color.darkgray));
							ib_hqyzm.setText("获取验证码");
						}
					}
				}
				break;
			}
		}
	};

	@Override
	protected void parseData(final String xml) {
		      LoadDialog.closeDialog();
		      Log.d("bbb", xml);
		switch (Utils.str2int(xml)) {
		case 1:
			ImeUtil.showToast(this, "登录成功", 2000);
		
			finish();
			break;
		case 0:
			ImeUtil.showToast(this, "账号或密码不正确", 2000);
			break;
		case -1:
			ImeUtil.showToast(this, "用户不存在", 2000);
			break;
		case -2:
			ImeUtil.showToast(this, "登录异常", 2000);
			break;

		default:
			break;}
	}
}
